﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;

namespace patients
{
    class Patient
    {
        public long id;
        public string name;
        public string surName;
        public string patronymis;
        public DateTime bod;
    }
    class Program
    {
        static void Main(string[] args)
        {
            string[] mas = new string[] { "", "", "", "", "", "", "", "" };
            string strAdd = "", strDelete = "", strEdit = "", strShow = "";
            int ID = instID();
            //mas[0] = "-edit";
            //mas[1] = "-33";
            mas[2] = "-Vova";
            if (args.Length == 0)
            {
                Console.WriteLine("If ADD enter: [-add] [-name] [-surname] [-patronymic] [-dob]");
                Console.WriteLine("If DELETE enter: [-delete] [-ID]");
                Console.WriteLine("If EDIT enter: [-edit] [-ID] [-name] [-surname] [-patronymic] [-dob]");
                Console.WriteLine("If SHOW enter: [-show]");
            }
            else
            {
                for (int i = 0; i < args.Length; i++)
                {
                    mas[i] = args[i];
                }
                if (mas[0] == "-add")//добавление
                {
                    for (int i = 0; i<6; i++)
                    {
                        if (mas[i + 1] == "" || i == 5)
                        {
                            strAdd += (mas[i] + "\n");
                            i = 5;
                        }
                        else
                        {
                            if (mas[i] == "-add")
                            {
                                strAdd += ("\n-" + (ID+1) + ",");
                            }
                            else
                            {
                                strAdd += (mas[i] + ",");
                            }
                        }
                    }
                    File.AppendAllText(".\\..\\..\\db.txt", strAdd);
                    strAdd = "";
                }
                else if (mas[0] == "-delete")//удаление
                {
                    try
                    {
                        string boof = "";
                        strDelete = mas[1];
                        for (int i = 0; i < strDelete.Length; i++)
                        {
                            if (strDelete[i] != '-')
                            {
                                boof += strDelete[i];
                            }
                        }
                        ID = Convert.ToInt32(boof);
                        searchID(ID);
                    }
                    catch (InvalidCastException e)
                    {
                        Console.WriteLine("Entered incorrectly ID!!!");
                    }
                }
                else if (mas[0] == "-edit")//редактирование
                {
                    try
                    {
                        for (int i = 1; i < 7; i++)
                        {
                            if (mas[i + 1] == "" || i == 6)
                            {
                                strEdit += (mas[i]);
                                i = 7;
                            }
                            else
                            {
                                strEdit += (mas[i] + ",");
                            }
                        }
                        editString(strEdit);
                        //сделать замену полученно строки на имеющуюся по значению ID
                    }
                    catch (InvalidCastException e)
                    {
                        Console.WriteLine("Entered incorrectly ID!!!");
                    }
                }
                else if (mas[0] == "-show")//вывод
                {
                    showFile();
                }
                else
                {
                    Console.WriteLine("Invalid input!!!");
                }
   
            }
        }

        public static void searchID(int id)//поиск строки по ID и удаление строки
        {
            string line= "";
            string strdel = "";
            StreamReader file = new StreamReader(".\\..\\..\\db.txt");
            ArrayList arrL = new ArrayList();
            while ((line = file.ReadLine()) != null)
            {
                arrL.Add(line);
                int i = 0; 
                string boofSearch = "";
                while (line[i] != ',')
                {
                    if (line[i] != '-')
                    {
                        boofSearch += line[i];
                    }
                    i++;
                }
                if (Convert.ToInt32(boofSearch) == id)
                {
                    Console.WriteLine("Line to delete: " + line);
                    strdel = line;
                }
            }
            file.Close();
            arrL.Remove(strdel);
            //записать лист в файл.
            File.WriteAllText(".\\..\\..\\db.txt", "");
            for (int i = 0; i < arrL.Count; i++)
            {
                File.AppendAllText(".\\..\\..\\db.txt", arrL[i].ToString() + "\n");
            }
        }

        public static void showFile()//вовод содержимого с файла
        {
            string line = "";
            StreamReader file = new StreamReader(".\\..\\..\\db.txt");
            while ((line = file.ReadLine()) != null)
            {
               Console.WriteLine(line);             
            }
            file.Close();
        }

        public static int instID()//поиск последнего ID;
        {
            string line = "";
            string boof = "";
            int id = 0, i = 0;
            StreamReader file = new StreamReader(".\\..\\..\\db.txt");
            while ((line = file.ReadLine()) != null)
            {
                while (line[i] != ',')
                {
                    if (line[i] != '-')
                    {
                        boof += line[i];
                    }
                    i++;
                }
                int n = Convert.ToInt32(boof);
                if (n > id)
                {
                    id = n;
                }
                i = 0;
                boof = "";
            }
            file.Close(); ;
            return id;
        }

        public static void editString(string str)//поиск и замена строки
        {
            int idNew = 0, idOld = 0, i = 0; 
            string line = "";
            string boof = "";
            StreamReader file = new StreamReader(".\\..\\..\\db.txt");
            ArrayList arrL = new ArrayList();
            //определение ID строки для редактирования
            while (str[i] != ',')
            {
                if (str[i] != '-')
                {
                    boof += str[i];
                }
                i++;
            }
            while ((line = file.ReadLine()) != null)
            {
                i = 0;
                string boofEdit = "";
                while (line[i] != ',')
                {
                    if (line[i] != '-')
                    {
                        boofEdit += line[i];
                    }
                    i++;
                }
                if (boofEdit == boof)
                {
                    Console.WriteLine("Edit OK!!!");

                    line = str;
                }
                arrL.Add(line);
            }
            file.Close();
            //записать лист в файл.
            File.WriteAllText(".\\..\\..\\db.txt", "");
            for (i = 0; i < arrL.Count; i++)
            {
                File.AppendAllText(".\\..\\..\\db.txt", arrL[i].ToString() + "\n");
            }
        }

        //
    }
}
/*
-1,-oi,-6987542
-20,-d,-9,-3
-33,-ppppp,-iiiii,-77777
-34,-oi,-6987542
-45,-d,-9,-3
-55,-ppppp,-iiiii,-77777
-57,-ppppp,-iiiii,-77777
*/